package com.hrm.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hrm.dao.ConfigItemMapper;
import com.hrm.dao.ConfigMapper;
import com.hrm.entity.ConfigItem;
import com.hrm.entity.Config;
import com.hrm.service.ConfigService;
import com.hrm.util.Page;

public class ConfigServiceImpl implements ConfigService {
	private static final Log log = LogFactory.getLog(ConfigServiceImpl.class); 
	@Autowired
	private ConfigMapper configMapper;
	@Autowired
	private ConfigItemMapper configItemMapper;
	public ConfigItemMapper getConfigItemMapper() {
		return configItemMapper;
	}
	
	public void setConfigItemMapper(ConfigItemMapper configItemMapper) {
		this.configItemMapper = configItemMapper;
	}
	public ConfigMapper getConfigMapper() {
		return configMapper;
	}

	public void setConfigMapper(ConfigMapper configMapper) {
		this.configMapper = configMapper;
	}
	
	public List<ConfigItem> getConfigItems() {
		log.info("****** Do method getConfigItems() begin,no params info.");
		List<ConfigItem> configItem=configItemMapper.selectAllConfigItem();
		log.info("****** Do method getConfigItems() end,return info ==== [ List<ConfigItem>="+configItem+" ]");
		return configItem;
	}
	public ConfigItem getConfigItemById(long id) {
		log.info("****** Do method getConfigItemById(long id) begin,params info ==== [ id="+id+" ]");
		ConfigItem configItem =  configItemMapper.selectConfigItemById(id);
		log.info("****** Do method getConfigItemById(long id) end,return info ==== [ ConfigItem="+configItem+" ]");
		return configItem;
	}
	
	public int addConfigItem(ConfigItem configItem) {
		log.info("****** Do method addConfigItem(ConfigItem configItem) begin,params info ==== [ configItem="+configItem+" ]");
		int cnts=-1;
		cnts= configItemMapper.insertConfigItem(configItem);
		log.info("****** Do method addConfigItem(ConfigItem configItem) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	
	public int updateConfigItem(ConfigItem configItem) {
		log.info("****** Do method updateConfigItem(ConfigItem configItem) begin,params info ==== [ configItem="+configItem+" ]");
		int cnts=-1;
		cnts= configItemMapper.updateConfigItem(configItem);
		log.info("****** Do method updateConfigItem(ConfigItem configItem) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	
	public int deleteConfigItemById(String ids) {
		log.info("****** Do method deleteConfigItemById(String ids) begin,params info ==== [ ids="+ids+" ]");
		Map<String,Object> map = new HashMap<String,Object>();
		String[] tmp=ids.split(",");
		Integer[] tids = new Integer[tmp.length];
		for(int i=0;i<tmp.length;i++){
			tids[i]=Integer.parseInt(tmp[i]);
		}
		List<Integer> configItems=Arrays.asList(tids);
		map.put("ids", configItems);
		int cnts=-1;
		cnts=configItemMapper.deleteConfigItemByMap(map);
		log.info("****** Do method deleteConfigItemById(String ids) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	public List<ConfigItem> getConfigItems(Page page) {
		log.info("****** Do method getConfigItems(Page page) begin,params info ==== [page="+page+"]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<ConfigItem> configItems = configItemMapper.selectConfigItemByMap(map);
		log.info("****** Do method getConfigItems(Page page) end,return info ==== [ List<ConfigItem>="+configItems+" ]");
		return configItems;
	}
	
	public int getConfigItemCounts() {
		log.info("****** Do method getConfigItemCounts() begin,no params info.");
		int cnts=-1;
		cnts= configItemMapper.getAllConfigItemCounts();
		log.info("****** Do method getConfigCounts() end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<Config> getConfigs() {
		log.info("****** Do method getConfigs() begin,no params info.");
		List<Config> configs=configMapper.selectAllConfig();
		log.info("****** Do method getRoles() end,return info ==== [ List<Config>="+configs+" ]");
		return configs;
	}
	public Config getConfigById(long id) {
		log.info("****** Do method getConfigById(long id) begin,params info ==== [ id="+id+" ]");
		Config config =  configMapper.selectConfigById(id);
		log.info("****** Do method getConfigById(long id) end,return info ==== [ Config="+config+" ]");
		return config;
	}

	public int addConfig(Config config) {
		log.info("****** Do method addConfig(Config config) begin,params info ==== [ config="+config+" ]");
		int cnts=-1;
		cnts= configMapper.insertConfig(config);
		log.info("****** Do method addConfig(Config config) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int updateConfig(Config config) {
		log.info("****** Do method updateConfig(Config config) begin,params info ==== [ config="+config+" ]");
		int cnts=-1;
		cnts= configMapper.updateConfig(config);
		log.info("****** Do method updateConfig(Config config) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteConfigById(long id) {
		log.info("****** Do method deleteConfigById(long id) begin,params info ==== [ id="+id+" ]");
		int cnts=-1;
		cnts= configMapper.deleteConfigById(id);
		log.info("****** Do method deleteConfigById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	

	public List<Config> getConfigs(Page page) {
		log.info("****** Do method getConfigs(Page page) begin,params info ==== [page="+page+"]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<Config> configs = configMapper.selectConfigByMap(map);
		log.info("****** Do method getConfigs(Page page) end,return info ==== [ List<Config>="+configs+" ]");
		return configs;
	}

	public int getConfigCounts() {
		log.info("****** Do method getConfigCounts() begin,no params info.");
		int cnts=-1;
		cnts= configMapper.getAllConfigCounts();
		log.info("****** Do method getConfigCounts() end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteConfigById(String ids) {
		log.info("****** Do method deleteConfigById(String ids) begin,params info ==== [ ids="+ids+" ]");
		Map<String,Object> map = new HashMap<String,Object>();
		String[] tmp=ids.split(",");
		Integer[] tids = new Integer[tmp.length];
		for(int i=0;i<tmp.length;i++){
			tids[i]=Integer.parseInt(tmp[i]);
		}
		List<Integer> configs=Arrays.asList(tids);
		map.put("ids", configs);
		int cnts=-1;
		cnts=configMapper.deleteConfigByMap(map);
		log.info("****** Do method deleteConfigById(String ids) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<ConfigItem> getConfigItemsByType(Page page,String type) {
		log.info("****** Do method getConfigItems(Page page,String type) begin,params info ==== [page="+page+",type="+type+"]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		map.put("configType", type);
		List<ConfigItem> configItems = configItemMapper.selectConfigItemByMap(map);
		log.info("****** Do method getConfigItems(Page page,String type) end,return info ==== [ List<ConfigItem>="+configItems+" ]");
		return configItems;
	}

	public int getConfigItemsCountsByType(String type) {
		log.info("****** Do method getConfigItemsCountsByType() begin,no params info.");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("configType", type);
		int cnts=-1;
		cnts= configItemMapper.getConfigItemCountsByMap(map);
		log.info("****** Do method getConfigItemsCountsByType() end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public List<ConfigItem> getConfigItemsByType(String type) {
		log.info("****** Do method getConfigItemsByType(String type) begin,params info ==== [type="+type+"]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("configType", type);
		List<ConfigItem> configItems = configItemMapper.selectConfigItemByMap(map);
		log.info("****** Do method getConfigItems(Page page,String type) end,return info ==== [ List<ConfigItem>="+configItems+" ]");
		return configItems;
	}

	public boolean isExistConfigItem(String configType, String configCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("configType", configType);
		map.put("configCode", configCode);
		return configItemMapper.selectConfigItemByMap(map).size()>0?true:false;
	}



}
